package com.hong.utils.semaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: Administrator
 * @Date: 2018/7/23 11:49
 * @Description:不可被打断的去获取信号量
 */
public class SemaphoreExample3 {
    public static void main(String[] args) throws InterruptedException {
        final Semaphore semaphore = new Semaphore(1);
        System.out.println(semaphore.isFair());
        Thread t1  = new Thread(){
            @Override
            public void run() {
                try {
                    semaphore.acquire();
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
                System.out.println("T1 finished.");
            }
        };

        t1.start();

        TimeUnit.MILLISECONDS.sleep(50);

        Thread t2  = new Thread(){
            @Override
            public void run() {
                try {
                    semaphore.acquireUninterruptibly();
                    /*TimeUnit.SECONDS.sleep(5);*/
                }finally {
                    semaphore.release();
                }
                System.out.println("T2 finished.");
            }
        };

        t2.start();

        TimeUnit.MILLISECONDS.sleep(50);
        t2.interrupt();
    }
}
